package com.ulook.lookplanet.ui.home.login;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import android.app.Application;
import android.util.Patterns;

import com.ulook.lookplanet.base.BaseViewModel;
import com.ulook.lookplanet.data.repository.LoginRepository;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.data.model.TUser;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

public class LoginViewModel extends BaseViewModel {

    private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
    private MutableLiveData<LoginResult> loginResult = new MutableLiveData<>();
    private LoginRepository loginRepository;

    // 构造函数
    LoginViewModel(LoginRepository loginRepository, Application application) {
        super(application);
        this.loginRepository = loginRepository;
        loginRepository.logout();  // 清除原有的登陆数据
    }

    LiveData<LoginFormState> getLoginFormState() {
        return loginFormState;
    }

    LiveData<LoginResult> getLoginResult() {
        return loginResult;
    }

    /**
     * 登陆动作
     */
    public void login(String username, String password) {
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                TUser user = (TUser)res;
                loginRepository.setLoggedInUser(user);
                loginResult.setValue(new LoginResult(user));
            }

            @Override
            public void onFailure(OkHttpException failuer) {
                loginResult.setValue(new LoginResult("登陆失败"));
                ThrowException.print(failuer,  "请求失败，"+failuer.getEmsg());
            }
        };
        loginRepository.login(username, password, callback);
    }

    /**
     * 注册账号
     */
    public void register(String nickname, String username, String password){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                TUser user = (TUser)res;
                loginRepository.setLoggedInUser(user);
                loginResult.setValue(new LoginResult(user));
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                loginResult.setValue(new LoginResult(failuer.getEmsg()));
                ThrowException.print(failuer,  "请求失败，"+failuer.getEmsg());
            }
        };
        loginRepository.register(nickname, username, password, callback);
    }


    /**
     * 登陆数据改变
     * @param username
     * @param password
     */
    public void loginDataChanged(String username, String password, String nickname) {
        if (!isUserNameValid(username)) {
            loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
        } else if (!isPasswordValid(password)) {
            loginFormState.setValue(new LoginFormState(null, R.string.invalid_password));
        } else if (!isNickNameValid(nickname)){
            loginFormState.setValue(new LoginFormState(R.string.invalid_nickname, null, null));
        } else {
            loginFormState.setValue(new LoginFormState(true));
        }
    }

    // 昵称验证检查
    private boolean isNickNameValid(String nickname){
        return nickname != null && nickname.length() <= 10;
    }

    // 用户名验证检查
    private boolean isUserNameValid(String username) {
        if (username == null) {
            return false;
        }
        if (username.contains("@")) {
            return Patterns.EMAIL_ADDRESS.matcher(username).matches();
        } else {
            return !username.trim().isEmpty();
        }
    }

    // 密码验证检查
    private boolean isPasswordValid(String password) {
        return password != null && password.trim().length() >= 3;
    }
}